class Queue {
    items: object;
    front: number; // 头指针
    rear: number; // 尾指针
    constructor() {
        this.items = {};
        this.front = 0;
        this.rear = 0;
    }
    //  入队
    enqueue(element) {
        this.items[this.rear++] = element;
    }
    // 出队
    dequeue() {
        if (!this.isEmpty()) {
            const element = this.items[this.front];
            delete this.items[this.front++]
            return element
        } else {
            console.warn('队列已经空啦！')
        }
    }
    peek() {
        return this.items[this.front]
    }
    isEmpty() {
        return this.size() === 0
    }
    size() {
        return this.rear - this.front
    }
}

class DQueuq {
    items: object;
    front: number;
    rear: number;
    constructor() {
        this.items = {};
        this.front = 0;
        this.rear = 0;
    }
    //  从前端添加
    addFront(element) {
        //  若队列为空， 则直接加在队尾
        if (this.isEmpty()) {
            this.addRear(element)
        }
        //  若 队首指针大于0 则 队首指针前移一位添加元素
        else if (this.front > 0) {
            this.items[--this.front] = element
        }
        //  若 队尾指针等于0 则将队列元素全部后移一位，将元素添加到队尾
        else {
            for (let index = this.size(); index > 0; index--) {
                this.items[index] = this.items[index - 1];
            }
            this.rear++;
            this.items[this.front] = element
        }
    }
    //  从后端添加
    addRear(element) {
        this.items[this.rear++] = element
    }
    //  从前端删除
    deFront() {
        if (!this.isEmpty()) {
            const element = this.items[this.front];
            delete this.items[this.front++]
            return element
        } else {
            console.warn('队列已经空啦！')
        }
    }
    //  从后端删除
    deBack() {
        if (!this.isEmpty()) {
            const element = this.items[--this.rear];
            delete this.items[this.rear]
            return element
        } else {
            console.warn('队列已经空啦！')
        }
    }
    //  队列是否为空
    isEmpty() {
        return this.size() === 0
    }
    //  队列长度
    size(): number {
        return this.rear - this.front
    }
    //  队首元素
    peekFront() {
        if (this.isEmpty()) {
            console.warn('队列已经么有元素啦！')
        } else {
            return this.items[this.front]
        }
    }
    //  队尾元素
    peekRear() {
        if (this.isEmpty()) {
            console.warn('队列已经么有元素啦！')
        } else {
            return this.items[this.rear - 1]
        }

    }
}



function hotPotato(list, number) {
    const queue = new Queue();
    list.forEach(item => {
        queue.enqueue(item)
    });

    while (queue.size() > 1) {
        for (let index = 1; index <= number; index++) {
            let item = queue.dequeue();
            queue.enqueue(item)
        }
        queue.dequeue()
    }
    return {
        winner: queue.dequeue()
    }
}

// let list =  ['John', 'Jack', 'Camila', 'Ingrid', 'Carl'];

// console.log(hotPotato(list , 7)) 

interface ArrayConstructor {
    from(arrayLike: any, mapFn?, thisArg?): Array<any>;
}

function palindromeChecker(str: String) {
    if (!str) {
        return ''
    }

    str = str.toLocaleLowerCase().split('  ').join('')
    const strDQueue = new DQueuq();
    Array.from(str).forEach(item => {
        strDQueue.addRear(item)
    })
    while (!strDQueue.isEmpty()) {
        if (strDQueue.peekFront() != strDQueue.peekRear()) {
            console.log(`${str}不是回文!`)
            return false
        }
        strDQueue.deFront()
        strDQueue.deBack()
    }
    console.log(`${str}是回文!`)
}

palindromeChecker('a')
palindromeChecker('kayak')
palindromeChecker('level')
palindromeChecker('Was it a car or a cat I saw')
palindromeChecker('Step on no pets')